package com.qc31.monitorlib.ui.photo

import autodispose2.autoDispose
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.entity.PhotoInfo
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import com.qc31.monitorlib.base.BaseReportViewModel
import com.qc31.monitorlib.uitls.DataUtil
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/** @Author Jasper @Time 2020/12/10 @Desc 照片查询 */
class PhotosViewModel :
    BaseReportViewModel(isThirty = true, isSimpleThirty = true) {
    private val dataSub by lazy { PublishSubject.create<ArrayList<PhotoInfo>>() }
    fun dataObserver(): Observable<ArrayList<PhotoInfo>> {
        return dataSub.hide()
    }

    override fun getData(carId: String, startTime: String, endTime: String) {
        super.getData(carId, startTime, endTime)
        toastLoading(true)
        ServiceHelper.getRepository().getHistoryPhotos(
            carId, TimeUtil.instance.formatHms(startTime),
            TimeUtil.instance.formatEndHms(endTime)
        ).autoDispose(this).subscribe({
            toastLoading(false)
            if (isHasListData(it, R.string.toast_no_photo)) return@subscribe
            dataSub.onNext(it)
            DataUtil.mPhotoInfo = it
        }, error)
    }
}
